<?php
/**
 * Description
 *
 * PHP version 7
 *
 * @category Custom
 * @package  Custom
 * @author   ryu <mo5467@126.com>
 * @date     2020-04-28 01:07:34
 * @lastTime 2020-04-28 01:29:05
 * @license  https://opensource.org/licenses/MIT MIT License
 * @link     none
 */
namespace Design\Behavioral\Iterator;

use Iterator;
use Countable;

class BookList implements Countable,Iterator
{

    private $books = [];

    private $currentIndex = 0;


    public function addBook(Book $book)
    {
        $this->books[] = $book;

    }//end addBook()


    public function removeBook(Book $bookToRemove)
    {
        foreach ($this->books as $key => $book) {
            if ($book->getAuthorAndTitle() === $bookToRemove->getAuthorAndTitle()) {
                unset($this->books[$key]);
            }
        }

        $this->books = array_values($this->books);

    }//end removeBook()


    public function count() : int
    {
        return count($this->books);

    }//end count()


    public function current()
    {
        return $this->books[$this->currentIndex];

    }//end current()


    public function key()
    {
        return $this->currentIndex;

    }//end key()


    public function next()
    {
        $this->currentIndex++;

    }//end next()


    public function rewind()
    {
        $this->currentIndex = 0;

    }//end rewind()


    public function valid() : bool
    {
        return isset($this->books[$this->currentIndex]);

    }//end valid()


}//end class
